package data.service.impl;

import data.dao.BirdMapper;
import data.model.Bird;
import data.service.BirdService;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import util.MybatisUtil;
import util.PinyinUtil;

import java.io.File;

public class BirdServiceImpl implements BirdService {

	private Logger logger = Logger.getLogger(BirdServiceImpl.class);

	private BirdMapper birdMapper;

	private SqlSession mSqlSession;

	private static final String HB_ROOT_PATH="C:/Users/Julian/Desktop/常驻文件夹/黑石顶app";

/*	private static final String TWITTER_IMAGE_NAME = "/录音语图";

	private static final String IMAGE_NAME = "/照片";

	private static final String HB_TWITTER_ROOT_PATH = HB_ROOT_PATH + TWITTER_IMAGE_NAME;

	private static final String HB_IMAGE_ROOT_PATH = HB_ROOT_PATH + IMAGE_NAME;*/

	private static final String TWITTER_IMAGE_ENGLISH_NAME = "/twitter";

	private static final String IMAGE_ENGLISH_NAME = "/photo";

	private static final String HB_TWITTER_ROOT_ENGLISH_NAME = HB_ROOT_PATH + TWITTER_IMAGE_ENGLISH_NAME;

	private static final String HB_IMAGE_ROOT_ENGLISH_NAME = HB_ROOT_PATH + IMAGE_ENGLISH_NAME;

	public BirdServiceImpl() {
		PropertyConfigurator.configure(ClassLoader.getSystemResource("log4j.properties"));
		mSqlSession=MybatisUtil.getSession();
		birdMapper = mSqlSession.getMapper(BirdMapper.class);
	}

	@Override
	public void close() {
		MybatisUtil.closeSession(mSqlSession);
	}

	@Override
	public boolean updateBirdPaths() {
		File songRootFile = new File(HB_TWITTER_ROOT_ENGLISH_NAME);
		File[] songFileList=songRootFile.listFiles();
		if (songFileList != null) {
			for (File file : songFileList) {
				String filename = file.getName();
				String basePath = TWITTER_IMAGE_ENGLISH_NAME + "/" + filename;
				String songImagePath = basePath;
				String songPath = basePath;
				int id = Integer.valueOf(filename.substring(0, filename.indexOf('.')));
				Bird bird = new Bird();
				for (File f : file.listFiles()) {
					String name = f.getName();
					if (name.substring(name.length() - 3).equals("wav")) {
						songPath += "/" + name + ";";
					} else if (name.substring(name.length() - 3).equals("png")) {
						songImagePath += "/" + name + ";";
					}
				}
				bird.setId(id);
				bird.setSongImagePath(songImagePath);
				bird.setSongPath(songPath);
				birdMapper.updateBird(bird);
				mSqlSession.commit();
				logger.debug("update twitter image:" + songImagePath + "\t" + songPath);
			}
		}

		File imageRootFile = new File(HB_IMAGE_ROOT_ENGLISH_NAME);
		File[] imageFileList=imageRootFile.listFiles();
		if (imageFileList != null) {
			for (File file : imageFileList) {
				String filename=file.getName();
				String basePath = IMAGE_ENGLISH_NAME + "/" + filename;
				String imagePaths = basePath;
				int id = Integer.valueOf(filename.substring(0, filename.indexOf('.')));
				File[] listFiles = file.listFiles();
				Bird bird = new Bird();
				if (listFiles != null) {
					for (File f : listFiles) {
						String name = f.getName();
						imagePaths += "/" + name + ";";
						if (bird.getImageList() == null) {
							bird.setImageList(imagePaths.substring(0, imagePaths.length() - 1));
						}
					}
				}
				bird.setId(id);
				bird.setImagePaths(imagePaths);
				birdMapper.updateBird(bird);
				mSqlSession.commit();
				logger.debug("update image:" + bird.getImageList() + "\t" + bird.getImagePaths());
			}
		}
		return true;
	}

	@Override
	public boolean updateBirdPinyin() {
		Long count=birdMapper.getCount();
		for (int i = 1; i <= count; i++) {
			Bird bird = birdMapper.getBirdById(i);
			String   headPinyin = String.valueOf(PinyinHelper.toHanyuPinyinStringArray(bird.getName().charAt(0))[0].charAt(0));
			System.out.println(i+"\theadPinyin = " + headPinyin);
			bird.setHeadPinyin(headPinyin);
			birdMapper.updateBird(bird);
			mSqlSession.commit();
		}
		return true;
	}

	@Override
	public void changeChineseToEnglishPath() {


	}

	private void convertAllNamesByPath(String originRootPath) {
		File twitterOriginPath = new File(originRootPath);
		//首先转换路径名
		File[] listTwitter = twitterOriginPath.listFiles();
		for (File f :listTwitter) {
			String englishPath = getChangedPathName(f);

			logger.debug(f.getAbsolutePath() + "\t To:\t" + englishPath + "\t" + f.renameTo(new File(englishPath)));
			File[] listFiles = f.listFiles();
			if (listFiles != null) {
				for (File file : listFiles) {
					String englishFile = getChangedPathName(file);

					logger.debug(file.getAbsolutePath() + "\t To:\t" + englishFile + "\t" + file.renameTo(new File(englishFile)));
				}
			}
		}
	}

	private String getChangedPathName(File f) {
		String englishPath = PinyinUtil.getPinyinByString(f.getName());
		englishPath = f.getAbsolutePath().substring(0, f.getAbsolutePath().lastIndexOf('\\') + 1) + englishPath;
		return englishPath;
	}

}